package controllers

import (
	"encoding/json"
	"strconv"
	"strings"
	"time"

	"github.com/gin-gonic/gin"

	"demo0606/modules"
)

type Auth struct{}

func (auth Auth) AddAuth(c *gin.Context) {
	// type Auth struct {
	// 	AuthName    string `json:"auth_name"`
	// 	Description string `json:"description"`
	// 	Path        string `json:"path"`
	// 	Roles       string `json:"roles"`
	// 	AddTime     int    `json:"add_time"`
	// }
	authData := modules.Auth{}
	c.ShouldBindJSON(&authData)
	j, _ := json.Marshal(authData)
	println("auth_name22", string(j))
	t := time.Now().Unix()
	time2 := int(t)
	authData.AddTime = time2
	authData2 := []modules.Auth{}
	modules.DB.Where("auth_name like ?", authData.AuthName).Find(&authData2)
	if len(authData2) == 0 {
		err := modules.DB.Create(&authData).Error
		println("err", err)
		if err == nil {
			c.JSON(200, gin.H{
				"success": true,
				"message": "新增权限成功",
			})
		} else {
			c.JSON(500, gin.H{
				"success": false,
				"message": "新增权限失败",
			})
		}
	} else {
		c.JSON(500, gin.H{
			"success": false,
			"message": "新增权限失败,已有该数据",
		})
	}

}
func (auth Auth) DeleteAuth(c *gin.Context) {
	id := c.Query("id")
	idNum, _ := strconv.Atoi(id)
	authData := modules.Auth{
		Id: idNum,
	}
	err := modules.DB.Delete(&authData).Error
	if err == nil {
		c.JSON(200, gin.H{
			"success": true,
			"message": "删除权限",
			"id":      id,
		})
	} else {
		c.JSON(500, gin.H{
			"success": true,
			"message": "删除权限失败",
		})
	}

}
func (auth Auth) UpdateAuth(c *gin.Context) {
	authData := modules.Auth{}
	c.ShouldBindJSON(&authData)
	err := modules.DB.Updates(&authData).Error
	if err == nil {
		c.JSON(200, gin.H{
			"success": true,
			"message": "更新权限成功",
		})
	} else {
		c.JSON(500, gin.H{
			"success": false,
			"message": "更新权限失败",
		})
	}

}
func (auth Auth) GetAuthList(c *gin.Context) {
	page := c.DefaultQuery("page", "1")
	pageSize := c.DefaultQuery("pageSize", "10")
	authName := c.DefaultQuery("auth_name", "")
	authData := []modules.Auth{}
	p, _ := strconv.Atoi(page)
	ps, _ := strconv.Atoi(pageSize)
	p = p - 1
	var count int64
	if authName == "" {
		//.Limit(-1).Offset(-1)   解决查询不到总数的问题
		err := modules.DB.Preload("Roles").Limit(10).Offset(p * ps).Find(&authData).Limit(-1).Offset(-1).Count(&count).Error
		if err == nil {
			c.JSON(200, gin.H{
				"success":  true,
				"message":  "获取权限列表",
				"list":     authData,
				"page":     page,
				"pageSize": pageSize,
				"authName": authName,
				"count":    count,
			})
		} else {
			c.JSON(500, gin.H{
				"success": false,
				"message": "获取权限列表失败",
			})
		}
	} else {
		err := modules.DB.Where("auth_name LIKE ?", "%"+authName+"%").Find(&authData).Limit(-1).Offset(-1).Count(&count)
		if err == nil {
			c.JSON(200, gin.H{
				"success":  true,
				"message":  "获取权限列表",
				"list":     authData,
				"page":     page,
				"pageSize": pageSize,
				"authName": authName,
				"count":    count,
			})
		} else {
			c.JSON(500, gin.H{
				"success": false,
				"message": "获取权限列表失败",
			})
		}
	}
	println("authData", authData)

}

func (auth Auth) ChangeAuthRoles(c *gin.Context) {
	authId := c.PostForm("auth_id")
	roleId := c.PostForm("role_id")
	authIdNum, _ := strconv.Atoi(authId)
	roleIdArr := strings.Split(roleId, ",")
	authData := modules.RoleAuth{}
	err := modules.DB.Where("auth_id=?", authId).Delete(&authData).Error
	for _, v := range roleIdArr {
		authData := modules.RoleAuth{}
		roleId, _ := strconv.Atoi(v)
		authData.AuthId = authIdNum
		authData.RoleId = roleId
		modules.DB.Create(&authData)
	}
	if err != nil {
		c.JSON(200, gin.H{
			"success": false,
			"message": "修改角色失败",
		})
	} else {
		c.JSON(200, gin.H{
			"success": true,
			"message": "修改角色成功",
		})
	}
}
func (auth Auth) Guanlian(c *gin.Context) {
	article := []modules.Article{}
	err := modules.DB.Preload("ArticleCate").Find(&article).Error
	if err == nil {
		c.JSON(200, gin.H{
			"success": true,
			"message": "关联查询成功",
			"list":    article,
		})
	} else {
		c.JSON(200, gin.H{
			"success": false,
			"message": "关联查询失败",
		})
	}

}

func (auth Auth) Guanlian2(c *gin.Context) {
	// users := []modules.User{}
	roles := []modules.Role{}
	err := modules.DB.Preload("Users").Find(&roles).Error
	if err == nil {
		c.JSON(200, gin.H{
			"success": true,
			"message": "关联查询成功2",
			"list":    roles,
		})
	} else {
		c.JSON(200, gin.H{
			"success": false,
			"message": "关联查询失败",
		})
	}

}
